package ch.fusun.baron.farm.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.data.DataListener;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.farm.Farm;
import ch.fusun.baron.farm.FarmService;
import ch.fusun.baron.map.Tile;
import ch.fusun.baron.map.ui.gef.editpart.TileChild;
import ch.fusun.baron.map.ui.gef.editpart.TileChildrenProvider;

/**
 * Provides all farms for a tile
 */
public class FarmTileChildrenProvider implements TileChildrenProvider {

	private final List<DataListener> listeners = new LinkedList<DataListener>();
	private FarmService farmService;

	/**
	 * The injection constructor
	 */
	public FarmTileChildrenProvider() {
		// Do nothing
	}

	/**
	 * @param farmService
	 *            the injected farm service
	 */
	@Inject
	public void setCityService(FarmService farmService) {
		this.farmService = farmService;
		this.farmService.addDataListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Collection<TileChild> getChildren(Tile model) {
		Farm farm = farmService.getFarm(model);
		Collection<TileChild> children = new ArrayList<TileChild>();
		if (farm != null) {
			children.add(new FarmTileChild(farm));
		}
		return children;
	}

	@Override
	public void addListener(DataListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public void dataChanged(DataUpdate update) {
		for (DataListener listener : listeners) {
			listener.dataChanged(update);
		}
	}

}
